/*
 * SudokuFrame.java
 *
 * Created on 22.May.2010, 19:55:05
 */

package com.acargil.sudoku.view;

import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JSplitPane;

import com.acargil.sudoku.view.listeners.InputListener;
import com.acargil.sudoku.view.listeners.SelectionListener;

/**
 *
 * @author Oguzhan ACARGIL
 */
public class Frame extends javax.swing.JFrame implements ISudokuView{

	private static final long serialVersionUID = -5633268127780437823L;

	private InputListener inputListener;
	private SelectionListener mouseSelectionListener;
	
    private BoardPanel southPanel;
    private MenuBar menuBar;
    
    public Frame() {
        initComponents();
    }

    private void initComponents() {
	
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Basic Sudoku");
		setResizable(false);
		
		menuBar = new MenuBar();
		this.setJMenuBar(menuBar);
		
		southPanel = new BoardPanel();
        southPanel.setFocusable(true);
		southPanel.addKeyListener(inputListener);
		southPanel.addMouseListener(mouseSelectionListener);
		
		JPanel northPanel, panel = new JPanel();
		panel.setBorder(javax.swing.BorderFactory.createTitledBorder("Sudoku"));
		northPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		northPanel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		
		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, northPanel, southPanel);
		splitPane.setDividerSize(1);
		panel.add(splitPane);
		
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);

        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
		
        setVisible(true);
        pack();
    }

    public void setSelectedCell(int value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setCell(int x, int y, int value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public int getCell(int x, int y) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void clearCell(int x, int y) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void setAllCells(int[][] values) {
    	this.southPanel.repaint();
    }

    public int[][] getAllCells() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void clearAllCells() {
        this.southPanel.clearAllCells();
    }

//    public void displayMessage(String message, int messageType) {
//        
//        switch(messageType) {
//            case ISudokuView.ERROR_MESSAGE:
//                MessageFactory.errorMessage(message);
//                break;
//            case ISudokuView.WARNING_MESSAGE:
//                MessageFactory.warningMessage(message);
//                break;
//            case ISudokuView.INFORMATION_MESSAGE:
//                MessageFactory.informationMessage(message);
//                break;
//            default:
//                System.err.println("Error occured at displayMessage!");
//        }
//    }

    public void setSelectionCoordinates(int x, int y) {
        this.southPanel.setSelectionCoordinates(x, y);
    }

    public int getSelectedXIndex() {
        return this.southPanel.getSelectionXCoordinate();
    }

    public int getSelectedYIndex() {
        return this.southPanel.getSelectionYCoordinate();
    }

    public void shiftSelection(int shift) {
        this.southPanel.shiftSelection(shift);
    }
    
    @Override
    public File getSaveFile() {
    	int selection;
    	File selectedFile = null;
    	JFileChooser fileChooser = new JFileChooser();
    	
    	fileChooser.setMultiSelectionEnabled(false);
    	fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    	selection = fileChooser.showDialog(new JFrame(), "Save Game");
    	if (selection == JFileChooser.APPROVE_OPTION) {
			selectedFile = fileChooser.getSelectedFile();
		}
    	
    	return selectedFile;
    }
    
    @Override
    public File getLoadFile() {
    	int selection;
    	File selectedFile = null;
    	JFileChooser fileChooser = new JFileChooser();
    	
    	fileChooser.setMultiSelectionEnabled(false);
    	fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
    	selection = fileChooser.showDialog(new JFrame(), "Load Game");
    	if (selection == JFileChooser.APPROVE_OPTION) {
			selectedFile = fileChooser.getSelectedFile();
		}
    	
    	return selectedFile;
    }

	@Override
	public void addAboutListener(ActionListener instance) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addClearListener(ActionListener instance) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addExitListener(ActionListener instance) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addGenerateListener(ActionListener instance) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addInputListener(KeyListener instance) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addLoadListener(ActionListener instance) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addSaveListener(ActionListener instance) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void addSelectionListener(MouseListener instance) {
		// TODO Auto-generated method stub
		
	}

}
